#include <stdio.h>
#include <execinfo.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
using namespace std;
void handler(int sig)
{
    void  *array[10];
    size_t size;

    // get void*'s for all entries on the stack
    size = backtrace(array, 10);

    // print out all the frames to stderr
    fprintf(stderr, "Error: signal %d:\n", sig);
    backtrace_symbols_fd(array, size, STDERR_FILENO);
    exit(1);
}

void handler1(int sig)
{
    void  *array[10];
    size_t size;
    char **func_name_cache;

    size            = backtrace(array, 10);
    func_name_cache = backtrace_symbols(array, size);

    for (size_t i = 0; i < size; i++)
    {
        cout << func_name_cache[i] << endl;
    }

    free(func_name_cache);
}

void fun3()
{
    int *foo = (int *)-1;
    printf("%d\n", *foo);
}

void fun2()
{
    fun3();
}
void fun1()
{
    fun2();
}

int main(int argc, char **argv)
{
    signal(SIGSEGV, handler);
    fun1();
}
